define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recovery/order/index' + location.search,
                    add_url: 'recovery/order/add',
                    edit_url: 'recovery/order/edit',
                    view_url: 'recovery/order/view',
                    del_url: 'recovery/order/del',
                    multi_url: 'recovery/order/multi',
                    import_url: 'recovery/order/import',
                    table: 'recovery_order'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'typedata', title: __('Typedata'), searchList: {"0":__('Typedata 0'),"1":__('Typedata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'ordertime', title: __('Ordertime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE',
                            formatter:function(value,row,index){
                                return value+row.address_detail;
                            }
                        },
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"0":__('线下支付'), "1":__('线上支付')},
                            formatter:function(value,row,index){
                                value = "线上支付";
                                if(row.pay_type=="0"){
                                    value = "线下支付";
                                }
                                return value;
                            }
                        },
                        {field: 'status', title: __('Status'),
                            formatter:function(value,row,index){
                                if(value=='0') value = '<span class="text-primary"><i class="fa fa-circle"></i> '+__('Status 0')+'</span>';
                                if(value=='1') value = '<span class="text-success"><i class="fa fa-circle"></i> '+__('Status 1')+'</span>';
                                if(value=='2') value = '<span class="text-gray"><i class="fa fa-circle"></i> '+__('Status 2')+'</span>';
                                if(value=='3' && row.storagedata==1) value = '<span class="text-warning"><i class="fa fa-circle"></i> '+__('Status 3')+'</span>';
                                if(value=='4' && row.storagedata==1) value = '<span class="text-info"><i class="fa fa-circle"></i> '+__('Status 3')+'</span>';
                                if(value=='3' && row.storagedata==0) value = '<span class="text-warning"><i class="fa fa-circle"></i> '+__('待入库')+'</span>';
                                if(value=='4' && row.storagedata==0) value = '<span class="text-info"><i class="fa fa-circle"></i> '+__('待入库')+'</span>';
                                if(value=='5') value = '<span class="text-gray"><i class="fa fa-circle"></i> '+__('Status 5')+'</span>';
                                if(value=='6') value = '<span class="text-red"><i class="fa fa-circle"></i> '+__('Status 6')+'</span>';
                                return value;
                            }
                        },
                        {field: 'rider.nickname', title: __('Rider.nickname'), operate: 'LIKE'},
                        {field: 'rider.mobile', title: __('Rider.mobile'), operate: 'LIKE'},
                        {field: 'rider.mobile', title: __('Rider.mobile'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate: false, formatter: Table.api.formatter.datetime,datetimeFormat:'YYYY-MM-DD'},
                        {field: 'operate', title: __('Operate'), table: table,
                            formatter:function(value,row,index){
                                value = '' +
                                    '<a href="javascript:;"  data-href="/root.php/recovery/order/view/ids/'+row.id+'" class="btn btn-xs btn-success  btn-view" data-toggle="tooltip" title="" data-table-id="table" data-field-index="14" data-row-index="'+index+'" data-button-index="1" data-original-title="查看">查看</a>';
                                if(row.status==6 || row.status==0){
                                    value += '<a href="javascript:;" data-href="/root.php/recovery/order/designate/ids/'+row.id+'" class="btn btn-xs btn-danger btn-designate" style="margin-left: 5px;" data-toggle="tooltip" title="" data-table-id="table" data-field-index="14" data-row-index="'+index+'" data-button-index="2" data-original-title="指派">指派</a>';
                                }
                                if(row.evaluateContent!=''){
                                    value += '<a href="javascript:;" data-href="/root.php/recovery/order/evaluateView/ids/'+row.id+'" class="btn btn-xs btn-primary btn-comment" style="margin-left: 5px;" data-toggle="tooltip" title="" data-table-id="table" data-field-index="14" data-row-index="'+index+'" data-button-index="2" data-original-title="查看评论">查看评论</a>';
                                }

                                return value;
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
            /*导出添加文字*/
            $(".glyphicon.glyphicon-export.icon-share").before("导出");
            $(".glyphicon.glyphicon-export.icon-share").remove();
            $(".pull-right.search").remove();
            $("#table").on("click",".btn-view",function () {
                var url = $(this).data('href');
                Fast.api.open( url, '订单详情', {
                    callback:function(value){

                    }
                });
            })

            $("#table").on("click",".btn-comment",function () {
                var url = $(this).data('href');
                Fast.api.open( url, '查看评价', {
                    callback:function(value){

                    }
                });
            })

            /*订单指派*/
            $("#table").on("click",".btn-designate",function () {
                var url = $(this).data('href');
                Fast.api.open( url, '订单指派', {
                    callback:function(value){

                    }
                });
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        view: function (){
            var data = JSON.parse(goodInfo);
            Table.api.init({
                extend: {
                    index_url: 'recovery/order/index',
                    table: 'recovery_order'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                data: data,
                columns: [
                    [
                        {field: 'recovery_category_name', title: __('类型')},
                        {field: 'price', title: __('单价'),
                            formatter:function(value,row,index){
                                return value+row.unit;
                            }
                        },
                        {field: 'weight', title: __('重量')},
                        {field: 'total_price', title: __('金额'),
                            formatter:function(value,row,index){
                                return "￥"+value;
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
            /*导出添加文字*/
            $(".glyphicon.glyphicon-export.icon-share").remove();
            $(".pull-right.search").remove();
            $("#one > div > div.bootstrap-table > div.fixed-table-toolbar > div:nth-child(2)").remove();
            $(".keep-open.btn-group").remove();
            $(".fixed-table-pagination").remove();

            /*点击播放回收录音*/
            $(document).on("click",".playRecord",function () {
                var x = document.getElementById("record");
                x.play();
            })


        },
        evaluateview: function (){
            console.log(123);
        },
        designate: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/riderList' + location.search,
                    table: 'recovery_order'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                singleSelect: true,//禁止多选
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('工号'), operate: false},
                        {field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: __('姓名'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'riderapply.IDcard', title: __('身份证'),operate: false},
                        {field: 'signstatus', title: __('状态'), operate: false,
                            formatter:function(value,row,index){
                                if(value=='0') value = '<span class="text-gray"><i class="fa fa-circle"></i> '+__('离线')+'</span>';
                                if(value=='1') value = '<span class="text-success"><i class="fa fa-circle"></i> '+__('在线')+'</span>';
                                return value;
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
            $("#one > div > div.bootstrap-table > div.commonsearch-table > form > fieldset > div > div:nth-child(3) > div > button.btn.btn-success").text('搜索');
            /*指派表单*/
            Form.api.bindevent($("#designate-form"), function(data, ret){
                Toastr.success("成功");
            }, function(data, ret){
                // Toastr.error("失败");
            }, function(success, error){
                //bindevent的第三个参数为提交前的回调
                //如果我们需要在表单提交前做一些数据处理，则可以在此方法处理
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                //Form.api.submit(this, success, error);
                var riderId = Table.api.selectedids(table);
                console.log(riderId);
                if(riderId.length ==0) {
                    Toastr.error("请选择骑手");
                    return false;
                }
                $("#rider_user_id").val(riderId[0]);
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});